﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using sixgod.Enum;
using SqlSugar;

namespace sixgod.Model.WorkFlow
{
    /// <summary>
    /// 流程主表
    /// </summary>
    [SugarTable("WorkFlow")]
    public class WorkFlow:ModelBase
    {
        /// <summary>
        /// 模板Code
        /// </summary>
        public string TemplateCode { get; set; }

        /// <summary>
        /// 流程名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 类型 字典
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 发起人
        /// </summary>
        public string Initiator { get; set; }

        [Navigate(NavigateType.ManyToOne,nameof(Initiator))]
        public User.User InitUser { get; set; }


        /// <summary>
        /// 发起时间
        /// </summary>
        public DateTime? InitTime { get; set; }

        /// <summary>
        /// 当前节点
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public string CurrentNodeId { get; set; }

        [Navigate(NavigateType.OneToOne,nameof(CurrentNodeId))]
        public WorkFlowNode CurrentNode { get; set; }

        /// <summary>
        /// 流程等级
        /// </summary>
        public EnumWorkFlowLevel Level { get; set; }

        /// <summary>
        /// 结果 0 进行中，1结束，2拒绝回退 3撤回
        /// </summary>
        public int? Result { get; set; }

        /// <summary>
        /// 是否回退
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public bool IsReback { get; set; }

        /// <summary>
        /// 回退理由
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public string RebackRemark { get; set; }

        /// <summary>
        /// 回退人
        /// </summary>
        [SugarColumn(IsNullable = true)]
        public string RebackUserId { get; set; }

        [Navigate(NavigateType.ManyToOne,nameof(RebackUserId))]
        public User.User RebackUser { get; set; }

        /// <summary>
        /// 模型数据验证
        /// </summary>
        /// <returns></returns>
        public override (bool, string) Verify()
        {
            return (false, null);
        }

        public void Create(string creator, WorkFlowTemplate.WorkFlowTemplate workFlowTemplate)
        {
            Initiator = creator;
            base.Create(creator);
            InitTime = DateTime.Now;
            TemplateCode = workFlowTemplate.Id;
            Name = workFlowTemplate.Name;
            Type = workFlowTemplate.Type;
            Level = workFlowTemplate.Level;
            Result = 0;
        }

        public void ReBack(string remark,string userId)
        {
            IsReback = true;
            Result = 2;
            RebackRemark = remark;
            RebackUserId = userId;
        }
    }
}
